<?php

/*
	http://localhost/afeios/seoTools/login163.php
*/
header("Content-type: text/css; charset=gb2312");
$url="https://ssl.mail.163.com/entry/coremail/fcg/ntesdoor2?df=webmail163&from=web&funcid=loginone&iframe=1&language=-1&net=t&passtype=1&product=mail163&race=-2_-2_-2_db&style=-1&uid=afei_test001@163.com";

$userAgent="Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11";
$cookie_file = tempnam('./data','cookie');
$request='savelogin=0&url2=http%3A%2F%2Fmail.163.com%2Ferrorpage%2Ferr_163.htm&username=afei_test001&password=happy_123';

$options = array(
	CURLOPT_HEADER => 1,
	CURLOPT_NOBODY => 1,
	CURLOPT_USERAGENT=>$userAgent,
	
	CURLOPT_RETURNTRANSFER => 1
);

//echo $request;
$ch =curl_init($url);
curl_setopt_array($ch, $options);
//curl_setopt($ch, CURLOPT_PROXY,"ssl.mail.163.com:80");
//绕过HTTPS验证
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_PORT, 443);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt ($ch,CURLOPT_AUTOREFERER ,1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_file);

/*
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	'Content-Type: application/x-www-form-urlencoded',
	'Content-Length: ' . strlen($request),
	'Origin: http://mail.163.com',
	'Accept-Encoding:gzip,deflate,sdch',
	'Accept-Language:zh-CN,zh;q=0.8,en;q=0.6'
	)
);
*/
$result=curl_exec($ch);

if (curl_errno($ch)) {
	echo 'Error: ' . curl_error($ch);
}

//print_r(curl_getinfo($ch));
curl_close($ch); //get data after login

function getSid($res){
	preg_match("/x-ntes-mailentry-location: (.*)main.jsp\?sid=(.*)/",$res,$arr);
	return trim($arr[2]);
}
$sid=getSid($result);
echo $result;


$urlMain=sprintf("http://twebmail.mail.163.com/js4/main.jsp?sid=%s",$sid);
//echo $request;
$ch =curl_init($urlMain);
curl_setopt_array($ch, $options);
//curl_setopt($ch, CURLOPT_PROXY,"ssl.mail.163.com:80");
//绕过HTTPS验证
curl_setopt ($ch, CURLOPT_POST, 0);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_file);
$result=curl_exec($ch);
echo $result;
/*
//printf('<script type="text/javascript">top.location.href = "http://twebmail.mail.163.com/js4/main.jsp?sid=%s";</script>',$sid);
*/

//echo '<script type="text/javascript">alert("sendMail!");</script>';
//登录后的操作
//SendEmail to 1290657123@qq.com
$sendUrl=sprintf("http://twebmail.mail.163.com/js4/s?sid={$sid}&func=mbox:compose&cl_send=2&l=compose&action=deliver");
$requestEmail="var=%3C%3Fxml%20version%3D%221.0%22%3F%3E%3Cobject%3E%3Cstring%20name%3D%22id%22%3Ec%3A1343657413394%3C%2Fstring%3E%3Cobject%20name%3D%22attrs%22%3E%3Cstring%20name%3D%22account%22%3E%22afei_test001%22%26lt%3Bafei_test001%40163.com%26gt%3B%3C%2Fstring%3E%3Cboolean%20name%3D%22showOneRcpt%22%3Efalse%3C%2Fboolean%3E%3Carray%20name%3D%22to%22%3E%3Cstring%3E1290657123%40qq.com%3C%2Fstring%3E%3C%2Farray%3E%3Carray%20name%3D%22cc%22%2F%3E%3Carray%20name%3D%22bcc%22%2F%3E%3Cstring%20name%3D%22subject%22%3Ett%3C%2Fstring%3E%3Cboolean%20name%3D%22isHtml%22%3Etrue%3C%2Fboolean%3E%3Cstring%20name%3D%22content%22%3E%26lt%3Bdiv%20style%3D'line-height%3A1.7%3Bcolor%3A%23000000%3Bfont-size%3A14px%3Bfont-family%3Aarial'%26gt%3Bccc%26lt%3B%2Fdiv%26gt%3B%3C%2Fstring%3E%3Cint%20name%3D%22priority%22%3E3%3C%2Fint%3E%3Cboolean%20name%3D%22saveSentCopy%22%3Etrue%3C%2Fboolean%3E%3Cboolean%20name%3D%22requestReadReceipt%22%3Efalse%3C%2Fboolean%3E%3Cstring%20name%3D%22charset%22%3EGBK%3C%2Fstring%3E%3C%2Fobject%3E%3Cboolean%20name%3D%22returnInfo%22%3Efalse%3C%2Fboolean%3E%3Cstring%20name%3D%22action%22%3Edeliver%3C%2Fstring%3E%3Cint%20name%3D%22saveSentLimit%22%3E1%3C%2Fint%3E%3C%2Fobject%3E";
$ch=curl_init($sendUrl);
curl_setopt_array($ch, $options);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $requestEmail);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$result=curl_exec($ch);
echo $result;

if (curl_errno($ch)) {
	echo 'Error: ' . curl_error($ch);
}

//print_r(curl_getinfo($ch));
curl_close($ch); //get data after login












